#include <stdio.h>
#include "esp_log.h"
#include "driver/ledc.h"
#include "freertos/FreeRTOS.h"  // 此处顺序不能错
#include "freertos/task.h"

static void example_pwm_ledc_init(){
    // 定时器配置
    ledc_timer_config_t  pwm_config = {
        .clk_cfg = LEDC_AUTO_CLK ,
        .duty_resolution = LEDC_TIMER_13_BIT ,
        .freq_hz = 1000,
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .timer_num = LEDC_TIMER_0,        
    };

    ledc_timer_config(&pwm_config);

    // 通道配置
    ledc_channel_config_t channel_config = {
        .channel = LEDC_CHANNEL_0,
        .duty = 4096,
        .gpio_num = 48,
        .intr_type = LEDC_INTR_DISABLE,
        .speed_mode = LEDC_LOW_SPEED_MODE,
        .timer_sel = LEDC_TIMER_0,
    };

    ledc_channel_config(&channel_config);
}

void example_pwm_ledc_test(){
    // pwm初始化
    example_pwm_ledc_init();
    // 修改占空比
    uint32_t i = 0;
    while(1){
        ledc_set_duty(LEDC_TIMER_0,LEDC_CHANNEL_0,i);
        ledc_update_duty(LEDC_TIMER_0,LEDC_CHANNEL_0);
        vTaskDelay(pdMS_TO_TICKS(10));

        i+=100;
        if(i > 8192) i = 0;
    }
}

